package io.zhengsh.juc._2synchronized1;

import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.TimeUnit;

public class SynchronizedVariableTest {

    public static void main(String[] args) throws InterruptedException {
        SynchronizedVariableTest test = new SynchronizedVariableTest();
        System.out.println("new Object ...");
        System.out.println(ClassLayout.parseInstance(test).toPrintable());

        Runnable r = () -> {
            System.out.println(Thread.currentThread().getName() + ": start Thread ...");
            System.out.println(ClassLayout.parseInstance(test).toPrintable());
            synchronized (test) {
                System.out.println(Thread.currentThread().getName() + ": obtain lock ...");
                System.out.println(ClassLayout.parseInstance(test).toPrintable());
                System.out.println(1);
            }
        };

        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();

        TimeUnit.SECONDS.sleep(5);
    }
}
